<?php
/**
 * @see Epic_Registry_Abstract
 */
require_once 'Epic/Registry/Abstract.php';

/**
 * Registry class to pass global variables between classes.
 * 
 * @category   Epic
 * @package    Epic_Registry
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
class Epic_Registry_Session extends Epic_Registry_Abstract
{
	private static $_instance = null;
	
	public static function getInstance()
	{
		if (null === self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
     * Constructor
     *
     * Instantiate using {@link getInstance()}; Epic_Registry_Session is a singleton object.
     *
	 * Session start
     */	
	protected function __construct()
	{
		session_start();
	}

	/**
	 * Adds a new variable to the session.
	 *
	 * @param string|array $key Name of the session
	 * @param mixed $value Value of the session
	 */
	public static function set($key, $value = null)
	{
		if(is_array($key))
			foreach($key as $_key => $_value)
				$_SESSION[$_key] = $_value;
		else
			$_SESSION[$key] = $value;
	}
	 
	/**
	 * Returns the value of the specified $key in the session.
	 *
	 * @param string $key Name of the session
	 * @return mixed Value of the specified $key
	 */
	public static function get($key)
	{
		return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
	}
	
	/**
	 * Tests if given $key exists in session
	 *
	 * @param string $key
	 * @return bool
	 */
	public static function has($key)
	{
		return isset($_SESSION[$key]) ?  true : false;
	}
	
	/**
	 * Returns the whole session as an array.
	 *
	 * @return array Whole session
	 */
	public static function getAllSession()
	{
		return $_SESSION;
	}

	/**
	 * Removes a session from the registry.
	 *
	 * @param string $key Name of the session
	 * @return bool
	 */
	public static function unsetSession($key)
	{
		unset($_SESSION[$key]);
	}
	
	/**
	 * Removes all session from the Registry.
	 */
	public static function destroySession()
	{
		$_SESSION = array();	
		// Remove the session ID cookie
	  	if (isset($_COOKIE[session_name()]))
	        setcookie(session_name(), '', time() - 42000, '/');
		session_destroy();
	}
}